package cn.gzjp.modules.logis.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import cn.gzjp.common.utils.MyBeanUtils;
import cn.gzjp.common.config.Global;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.modules.logis.entity.AreaType;
import cn.gzjp.modules.logis.service.AreaTypeService;

/**
 * 树形地址Controller
 * @author zengcp
 * @version 2018-05-30
 */
@Controller
@RequestMapping(value = "${adminPath}/logis/areaType")
public class AreaTypeController extends BaseController {

	@Autowired
	private AreaTypeService areaTypeService;
	
	/**
	 * 树形地址列表页面
	 */
	@RequiresPermissions("logis:areaType:list")
	@RequestMapping(value = {"list", ""})
	public String list(AreaType areaType, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<AreaType> list = areaTypeService.findList(areaType); 
		model.addAttribute("list", list);
		return "modules/logis/areaTypeList";
	}

	/**
	 * 查看，增加，编辑树形地址表单页面
	 */
	@RequiresPermissions(value={"logis:areaType:view","logis:areaType:add","logis:areaType:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(AreaType areaType, Model model) {
		if (areaType.getParent()!=null && StringUtils.isNotBlank(areaType.getParent().getId())){
			areaType.setParent(areaTypeService.get(areaType.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(areaType.getId())){
				AreaType areaTypeChild = new AreaType();
				areaTypeChild.setParent(new AreaType(areaType.getParent().getId()));
				List<AreaType> list = areaTypeService.findList(areaType); 
				if (list.size() > 0){
					areaType.setSort(list.get(list.size()-1).getSort());
					areaType.setSort(areaType.getSort() + 30);
				}
			}
		}
		if (areaType.getSort() == 0){
			areaType.setSort(30);
		}
		model.addAttribute("areaType", areaType);
		return "modules/logis/areaTypeForm";
	}

	/**
	 * 保存树形地址
	 */
	@RequiresPermissions(value={"logis:areaType:add","logis:areaType:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(AreaType areaType, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, areaType)){
			return form(areaType, model);
		}
		if(!areaType.getIsNewRecord()){//编辑表单保存
			AreaType t = areaTypeService.get(areaType.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(areaType, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			areaTypeService.save(t);//保存
		}else{//新增表单保存
			areaTypeService.save(areaType);//保存
		}
		addMessage(redirectAttributes, "保存树形地址成功");
		return redirect("/logis/areaType/?repage");
	}
	
	/**
	 * 删除树形地址
	 */
	@RequiresPermissions("logis:areaType:del")
	@RequestMapping(value = "delete")
	public String delete(AreaType areaType, RedirectAttributes redirectAttributes) {
		areaTypeService.delete(areaType);
		addMessage(redirectAttributes, "删除树形地址成功");
		return redirect("/logis/areaType/?repage");
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<AreaType> list = areaTypeService.findList(new AreaType());
		for (int i=0; i<list.size(); i++){
			AreaType e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	
	@ModelAttribute
	public AreaType get(@RequestParam(required=false) String id) {
		AreaType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = areaTypeService.get(id);
		}
		if (entity == null){
			entity = new AreaType();
		}
		return entity;
	}
	
}